package com.wyw.netty.learning.bio;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * BIO 程序
 *
 * @Author： Mr Wu （yewen.wu.china.gmail.com）
 * @Date: 2021/2/3 16:09
 */
public class BIOEchoServer {

    public static void main(String[] args) throws IOException {
        //创建服务器，绑定8081端口
        ServerSocket serverSocket = new ServerSocket(8001);
        System.out.println("server start");

        while(true){
            //开始接收客户端的链接
            Socket socket = serverSocket.accept();

            System.out.println("one client conn: " + socket);
            new Thread(() -> {
                try {
                    BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                    String msg;
                    while((msg = reader.readLine()) != null){
                        System.out.println("receive msg:" + msg);
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }).start();

        }
    }
}
